<?php
namespace BoxEdu\Logic;
class ExamLogic extends \HomeController
{
    public function _initialize()
    {
        parent::_initialize();

    }

    /*********试卷评分*
     * array(2) {
     * ["examid"] => string(2) "13"
     * ["questionid"] => array(4) {
     * [9078] => array(1) {
     * [0] => string(1) "1"
     * }
     * [74] => array(1) {
     * [0] => string(1) "1"
     * }
     * [9044] => array(1) {
     * [0] => string(1) "0"
     * }
     * [8447] => array(1) {
     * [0] => string(1) "1"
     * }
     * }
     * }
     **/

    public function marking($data)
    {
        $examid = $data['examid'];
        // dump($data);exit();
        $exams_data = BoxModel("edu_exams")->load($examid);
        $count = $exams_data['count'];//数量
        $point = 0; //得分
        $right = 0; //正确数
        $wrong = 0; //错误数
        $not_done = 0; //未做题数
        $wrong_question_ids = array();//错误题ID
        $not_done_question_ids = array();//没做题数
        $right_question_ids = array();//做对的题数
        $point_all = $exams_data['examsetting']['score']; //总分
        $passscore = $exams_data['examsetting']['passscore']; //及格线
        $questions = array();
        foreach ($data['questionid'] as $k => $one) {
            $answer[$k] = implode(",", $one);
        }
        /******评分****/
        $question_data = $exams_data['examsetting']['questype'];
        foreach ($question_data as $one) {
            foreach ($one['examquestions'] as $one1) {
                $answer = array();
                $this->marking_one($one1['questionid'], $data['questionid'], $examid);
            }
        }
        $right_answer = $GLOBALS['answer'];
        // dump($right_answer);
        foreach ($right_answer as $k => $one) {
            // dump($one);
            if ($one['type'] == "right") {//做对的
                $point += $one['score'];
                $right++;
                $questions[$k] = "1";
                $right_question_ids[$k] = $k;

            } elseif ($one['type'] == "wrong") { //错题
                $wrong_question_ids[$k] = $k;
                $wrong++;
                $questions[$k] = "0";
            } else {//没做题
                $not_done_question_ids[$k] = $k;
                $not_done++;
                $questions[$k] = "0";
            }
        }

        /******错题加入到数据库存**/
        $wrong_questions = array_merge((array)$wrong_question_ids, (array)$not_done_question_ids);
        foreach ($wrong_questions as $qid) {
            $row = array(
                'questions_id' => $qid,
                'exams_id' => $examid,
                'type' => 'wrong',
            );
            $r = Logic("BoxEdu", "User")->collect_questions($row);
        }
        /******我的做题记录到数据库存*/
        $return = array(
            'point' => $point,
            'right' => $right,
            'wrong_question_ids' => $wrong_question_ids,
            'not_done_question_ids' => $not_done_question_ids,
            'right_question_ids' => $right_question_ids,
            'wrong' => $wrong,
            'examid' => $examid,
            'point_all' => $point_all,
            'passscore' => $passscore,
            'questions' => $questions,
            'not_done' => $not_done,
            'count' => $count,
            'answers' => $data['questionid'],
            'rate' => round(($right / $count) * 100, 2) . "%",
            'right_answer' => $right_answer,
        );
        /*******考试记录***/
        $r = BoxModel("edu_examhistory")->where("examid='$examid' and user_id='$this->user_id'")->find();
        $row = BoxModel("edu_examhistory")->create($exams_data);
        $row['user_id'] = $this->user_id;
        $row['marking'] = json_encode($return);
        $row['exams_data'] = json_encode($exams_data);
        if ($r) {
            BoxModel("edu_examhistory")->where("examid='$examid' and user_id='$this->user_id'")->save($row);
            $ehid = $r['ehid'];
        } else {
            $ehid = BoxModel("edu_examhistory")->add($row);
            /*******参加考试获得经验值***/
            $row = array(
                'num' => $exams_data['examsetting']['xuebazi'],
               // 'remark' => "完成：【" . $row['exam'] . "】测试获得" . $exams_data['examsetting']['xuebazi'] . "经验值",
                'subjectid' => $row['subjectid'],
																'title'=> $row['exam'],
																'url'=>HomeUrl.U('Subject/moni_info',array('subjectid'=>$row['subjectid'],'examid'=>$examid)),
																'event'=>'00001',
            );
            Logic("BoxEdu", "User")->add_xuebazi($row);
        }
        $return['ehid'] = $ehid;
        $return['exams_data'] = json_encode($exams_data);
        return $return;
    }

    /********评一道题***/
    public function marking_one($q_id, $a, $examid, $questiontype)
    {
        $data = BoxModel("edu_questions")->load($q_id);
        $answer = $a[$q_id];//回答的答案
        $exam_data = BoxModel("edu_exams")->where(array('examid' => $examid))->find();
        $exam_data['examsetting'] = json_decode($exam_data['examsetting'], true);
        if (!$questiontype) {//冒题的话，分数从主题获得
            $questiontype = $data['questiontype'];
        }
        //dump($exam_data['examsetting']);exit();
        $score = $exam_data['examsetting']['questype'][$questiontype]['score'];//每题几分
        /*****冒题***/
        if ($data['questype']['questsort'] == 3) {
            $rows = BoxModel("edu_questions")->where("questionparent='$q_id'")->select();
            foreach ($rows as $one) {
                $this->marking_one($one['questionid'], $a, $examid, $questiontype);
            }
        } else {
            $this->do_marking_one($data, $answer, $score);
        }
    }

    public function do_marking_one($data, $answer, $score)
    {
        /****questchoice为 1、2、3、4时为选择题******/
        if ($data['questype']['questchoice'] < 5) {
            foreach ($data['questionanswer'] as $k => $one) {
                if ($one['choose'] == 1) $right_answer[] = $k;
            }
            $right_answer = implode(",", $right_answer);
            $user_answer = implode(",", $answer);
            if ($right_answer == $user_answer) {
                $type = "right";
            } else {
                $type = "wrong";
            }
        }
        /****分录题**/
        if ($data['questype']['questchoice'] == 5) {
            $questionanswer = $data['questionanswer'];
            $type = "wrong";
            //////分录
            if ($answer['type'] == 1) {
                foreach ($questionanswer[1] as $k => $one) {
                    $key = md5($one['type'] . "_" . $one['money'] . "_" . $one['code']);
                    $right_answer[$key] = $key;
                }
                foreach ($answer['data'] as $one) {
                    $key = md5($one['type'] . "_" . $one['money'] . "_" . $one['code']);
                    $user_answer[$key] = $key;
                }
                ksort($right_answer);
                ksort($user_answer);
                $right_answer = implode(",", $right_answer);
                $user_answer = implode(",", $user_answer);
                if ($user_answer == $right_answer) {
                    $type = "right";
                }
            }
            //////金额计算
            if ($answer['type'] == 2 && $answer['sum'] == $questionanswer['2']['money']) {
                $type = "right";
            }
        }
        /*******其它题型**/
        $GLOBALS['answer'][$data['questionid']]['type'] = $type;
        $GLOBALS['answer'][$data['questionid']]['score'] = $score;    //这题得分
        $GLOBALS['answer'][$data['questionid']]['answer'] = $answer;//回答的答案
    }

    /************获得一个试卷*******/
    public function load($examid)
    {
        $history = BoxModel("edu_examhistory")->where("examid='$examid' and user_id='$this->user_id'")->find();
        if ($history) {
            $data = BoxModel("edu_exams")->load_history($history['ehid']);
        } else {
            $data = BoxModel("edu_exams")->load($examid);
        }
        return $data;
    }

    /*********获得试卷列表*******/
    public function get($where, $limit)
    {
        $data = BoxModel("edu_exams")->get($where, $limit);
        if ($limit) {
            $data = $data['list'];
        }
        foreach ($data as $k => $one) {
            $examid = $one['examid'];
            $history = BoxModel("edu_examhistory")->where("examid='$examid' and user_id='$this->user_id'")->find();
            if ($history) {
                $r = BoxModel("edu_exams")->load_history($history['ehid']);
                $data[$k]['marking'] = $r['marking'];
            } else {
                $data[$k]['marking']['count'] = $one['count'];
                $data[$k]['marking']['right'] = 0;
                $data[$k]['marking']['wrong'] = 0;
                $data[$k]['marking']['not_done'] = 0;
            }
            $data[$k]['marking']['done'] = $data[$k]['marking']['right'] + $data[$k]['marking']['wrong'];

        }

        return $data;
    }


}